<script lang="ts" setup>
import type { FindBannerType } from '@/types/home'
import { ref, withDefaults, watchEffect, onUnmounted } from 'vue'
// 接收父组件传入的值，并且配置默认值
const props = withDefaults(
  defineProps<{
    // 渲染数据
    sliders: FindBannerType[]
    // 轮播间隔时间
    duration?: number
    // 是否开启自动播放
    autoPlay?: boolean
  }>(),
  {
    duration: 2000,
    autoPlay: true
  }
)
// 显示图片的默认索引
const index = ref(0)
// 自动播放逻辑
let time: number | null = null
const autoPlayFn = () => {
  if (time) clearInterval(time)
  time = setInterval(() => {
    // 图片按照索引开始自增实现图片切换
    index.value++
    if (index.value >= props.sliders.length) {
      // 当自增超过渲染数组长度时，返回重新渲染第一张图片
      index.value = 0
    }
  }, props.duration)
}
watchEffect(() => {
  // 有数据，而且开启了自动播放
  if (props.sliders.length && props.autoPlay) {
    index.value = 0
    autoPlayFn()
  }
})
// 鼠标移入移出控制图片切换
const stop = () => {
  // 清空定时器，暂停切换
  if (time) clearInterval(time)
}
const start = () => {
  if (props.sliders.length && props.autoPlay) {
    autoPlayFn()
  }
}
// 点击按钮实现上下图片切换
const toggle = (val: number) => {
  if (val === 1) {
    if (index.value >= props.sliders.length - 1) return (index.value = 0)
    index.value++
  } else if (val === -1) {
    if (index.value < 1) return (index.value = props.sliders.length - 1)
    index.value--
  }
}
// 组件销毁，清除定时器
onUnmounted(() => {
  clearInterval(time!)
})
</script>

<template>
  <div class="xtx-carousel" @mouseenter="stop()" @mouseleave="start()">
    <ul class="carousel-body">
      <li
        class="carousel-item"
        v-for="(item, i) in sliders"
        :key="i"
        :class="{ fade: index === i }"
      >
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <a href="javascript:;" class="carousel-btn prev" @click="toggle(-1)"
      ><i class="iconfont icon-angle-left"></i
    ></a>
    <a href="javascript:;" class="carousel-btn next" @click="toggle(1)"
      ><i class="iconfont icon-angle-right"></i
    ></a>
    <div class="carousel-indicator">
      <span
        v-for="(item, i) in sliders"
        :key="i"
        :class="{ active: index === i }"
      ></span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
